package edu.fiu.cis.personalize.server.graph;

import java.io.Serializable;
import java.util.concurrent.atomic.AtomicInteger;

import edu.fiu.cis.personalize.server.graph.store.CountingConcMap;
import edu.fiu.cis.personalize.server.graph.store.NodeEdges;

//A Query object has the attribute "query" which keeps the submitted query by
//user
@SuppressWarnings("serial")
public class Query extends Node implements Serializable {

	private CountingConcMap submittedByUser = new CountingConcMap();
	private CountingConcMap displaysDocuments = new CountingConcMap();

	/**
	 * 
	 * @param query
	 */
	public Query(int id, String query) {
		super(id, query);
	}

	/**
	 * 
	 * @return The submitted query
	 */
	public String getQuery() {
		return getStringKey();
	}

	public String toString() {
		return getStringKey();
	}

	@Override
	public NodeType getType() {
		return NodeType.QUERY;
	}

	public int submittedByUser(User u) {
		return submittedByUser.incrementCount(u.getId());
	}

	public int displaysDocument(Document d) {
		return displaysDocuments.get(d.getId()).incrementAndGet();
	}

	public CountingConcMap getDisplaysDocuments() {
		return displaysDocuments;
	}

	public CountingConcMap getSubmittedByUser() {
		return submittedByUser;
	}

	public static Query create(int id, String gname, NodeEdges nodeEdges,
			double rank, CountingConcMap dd, CountingConcMap su) {
		Query q = new Query(id, gname);
		q.edges = nodeEdges;
		q.rank = rank;
		q.displaysDocuments = dd;
		q.submittedByUser = su;
		return q;
	}

	public double getAverageUserSubmissions() {
		double a = 0;
		for (AtomicInteger i : submittedByUser.values()) {
			a += i.doubleValue();
		}

		return a / submittedByUser.size();
	}

}
